package com.school.express.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.express.domain.Express;
import com.school.express.domain.Logistics;
import com.school.express.domain.UserExpress;
import com.school.express.mapper.ExpressMapper;
import com.school.express.mapper.LogisticsMapper;
import com.school.express.mapper.UserExpressMapper;
import com.school.express.model.ExpressModel;
import com.school.express.service.ExpressService;
import zhibi.common.support.base.Page;
import zhibi.common.support.base.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;

import java.util.Date;

/**
* The class create by 执笔 on generate.
*/
@Service
@Transactional
public class ExpressServiceImpl extends BaseServiceImpl<Express> implements ExpressService{

    @Autowired
    public ExpressServiceImpl(ExpressMapper mapper) {
        super(mapper);
        this.expressMapper = mapper;
    }
    private ExpressMapper expressMapper;
    @Autowired
    private UserExpressMapper userExpressMapper;
    @Autowired
    private LogisticsMapper logisticsMapper;

    @Override
    public void chuku(UserExpress userExpress) {
        userExpress.setAddtime(new Date());
        Express express = expressMapper.selectByPrimaryKey(userExpress.getExpressId());
        if(null == express){
            throw new MessageException("该快递不存在");
        }
        express.setStatus(2);
        expressMapper.updateByPrimaryKey(express);
        userExpressMapper.insertSelective(userExpress);
        Logistics logistics = new Logistics();
        logistics.setAddtime(new Date());
        logistics.setExpressId(express.getId());
        logistics.setRemark("快递出库");
        logisticsMapper.insert(logistics);
    }

    @Override
    public PageInfo<Express> selectByUser(Example example, Page page) {
        if(null != page) PageHelper.startPage(page.getPageNum(),page.getPageSize());

        return new PageInfo<Express>(expressMapper.selectByUser(example),Page.NAV_SIZE) ;
    }

    @Override
    public PageInfo<ExpressModel> selectModel(Example example, Page page) {
        if(null != page) PageHelper.startPage(page.getPageNum(),page.getPageSize());

        return new PageInfo<ExpressModel>(expressMapper.selectModel(example),Page.NAV_SIZE) ;
    }

    @Override
    public void stock() {

    }
}